package com.ruoyi.project.system.service;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.project.system.domain.BeiDiao.Judiciary;
import com.ruoyi.project.system.domain.BeiDiao.Misconduct;
import com.ruoyi.project.system.domain.BeiDiao.Qualification;
import com.ruoyi.project.system.domain.BeiDiao.ThreeElement;
import com.ruoyi.project.system.domain.BeiDiaoBase;
import com.ruoyi.project.system.domain.dto.JudiciaryDto;
import com.ruoyi.project.system.domain.dto.QueryData;
import com.ruoyi.project.system.mapper.TzBeiDiaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.common.utils.http.HttpUtils.sendPost;
import static com.ruoyi.common.utils.uuid.Seq.getId;

@Service
public class BdReportService {

    @Autowired
    private TzBeiDiaoMapper tbdm;

    public Map<String, Object> fast(BeiDiaoBase bdb) {
        Map<String, Object> map = new HashMap<>();
        map.put("sys", queryData("JF007", bdb));
        map.put("sf", queryData("JF025", bdb));
        return map;
    }

    public Map<String, Object> ordinary(BeiDiaoBase bdb) {
        Map<String, Object> map = new HashMap<>();
        map.put("sys", queryData("JF007", bdb));
        map.put("sf", queryData("JF025", bdb));
        map.put("bl", queryData("JF002", bdb));
        return map;
    }


    public Map<String, Object> onboardingSurvey(BeiDiaoBase bdb) {
        Map<String, Object> map = new HashMap<>();
        map.put("JF007", queryData("JF007", bdb));
        map.put("JF025", queryData("JF025", bdb));
        map.put("JF003", queryData("JF003", bdb));
        return map;
    }

    public Object queryData(String jfCode, BeiDiaoBase bdb) {

        JSONObject params = new JSONObject();
        params.put("req_time", System.currentTimeMillis());
        params.put("request_sn", "TZ" + getId());
        params.put("inter_id", jfCode);

        if ("JF007".equals(jfCode)) {           // 三要素
            ThreeElement te = tbdm.selectBeiDiaoSys(bdb.getOrderNo());
            if (te == null) {
                params.put("name", bdb.getName());
                params.put("cardNo", bdb.getIdNumber());
                params.put("mobile", bdb.getTel());
                QueryData result = sendPost(params);
                if (result.isSuccess()) {
                    te = new ThreeElement(bdb.getOrderNo(), result);
                } else {
                    te = new ThreeElement(bdb.getOrderNo(), result.getMsg());
                }
                tbdm.insertBeiDiaoSys(te);
            }
            return te;
        } else if ("JF025".equals(jfCode)) {    // 司法涉诉
            JudiciaryDto judiciaryDto = tbdm.selectBeiDiaoSfss(bdb.getOrderNo());
            if (judiciaryDto == null) {
                params.put("name", bdb.getName());
                params.put("cardNo", bdb.getIdNumber());
                QueryData result = sendPost(params);
                String orderNo = bdb.getOrderNo();
                if (result.isSuccess()) {
                    JSONObject retdata = (JSONObject) result.getInfo();
                    if (1 == retdata.getIntValue("resultCode")) {
                        judiciaryDto = new JudiciaryDto(orderNo, result.getMsg(), retdata.getJSONArray("detail").getJSONObject(0));
                    } else {
                        judiciaryDto = new JudiciaryDto(orderNo, "2", retdata.getString("messge"));
                    }
                } else {
                    judiciaryDto = new JudiciaryDto(orderNo, "9", result.getMsg());
                }
                tbdm.insertBeiDiaoSfss(judiciaryDto);
            }
            return new Judiciary(judiciaryDto);
        } else if ("JF002".equals(jfCode)) {    // 不良行为
            Misconduct misconduct = tbdm.selectBeiDiaoBlxw(bdb.getOrderNo());
            if (misconduct == null) {
                params.put("name", bdb.getName());
                params.put("cardNo", bdb.getIdNumber());
                QueryData result = sendPost(params);
                misconduct = new Misconduct(bdb.getOrderNo(), result);
                tbdm.insertBeiDiaoBlxw(misconduct);
            }
            return misconduct;
        } else if ("JF003".equals(jfCode)) {
            Qualification qualification = tbdm.selectBeiDiaoXl(bdb.getOrderNo());
            if (qualification == null) {
                params.put("name", bdb.getName());
                params.put("cardNo", bdb.getIdNumber());
                QueryData result = sendPost(params);
                qualification = new Qualification(bdb.getOrderNo(), result);
                tbdm.insertBeiDiaoXl(qualification);
            }
            return qualification;
        } else {
            QueryData result = new QueryData("没有此产品");
            return result;
        }
    }

}
